package com.gears_software.bbnetwork.views;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EViewGroup;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.gears_software.bbnetwork.R;
import com.gears_software.bbnetwork.activitys.ShowWebViewActivity_;
import com.gears_software.bbnetwork.datas.models.ViwepagerModel;
import com.gears_software.bbnetwork.utlis.ImageFileCache;
import com.gears_software.common.net.avatarupload.UploadFileUtil;
import com.gears_software.common.trinea.services.impl.ImageMemoryCache;
/**
 * 轮播图片
 * @author DY-PC
 *
 */
@EViewGroup(R.layout.item_viewpager)
public class ViewpagerItemView extends LinearLayout {
	private ImageMemoryCache imageMemoryCache; // 内存缓存
	@ViewById
	ImageView viewpager_image;
	ViwepagerModel model;
	Context context;
	public ViewpagerItemView(Context context) {
		super(context);
		this.context = context;
		imageMemoryCache = new ImageMemoryCache();
	}
	
	public void bind(ViwepagerModel model){
		this.model = model;
		if(model!=null){
			Bitmap bit = ImageFileCache.getFileImage(model.getImgUrl());
			if(bit!=null){
				viewpager_image.setImageBitmap(bit);
			}else{
				getBitmap(model.getImgUrl());
			}
		}
	}
	
	@Click
	void viewpager_image(){
		if(model!=null){
			Intent intent = new Intent(context,ShowWebViewActivity_.class);
			intent.putExtra("url", model.getDetailUrl());
			context.startActivity(intent);
		}
	}
	
	@Background
	void getBitmap(String url){
		Bitmap bitmap = UploadFileUtil.httpsGetBitmap(url);
		ImageFileCache.saveFile(bitmap, url);
		setBitmap(bitmap);
	}
	
	@UiThread
	void setBitmap(Bitmap bitmap){
		viewpager_image.setImageBitmap(bitmap);
	}
}
